package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.entity.Setmeal;
import cn.usian.entity.SetmealDish;
import cn.usian.entity.SetmealDto;
import cn.usian.exception.NameException;
import cn.usian.mapper.SetmealDishMapper;
import cn.usian.mapper.SetmealMapper;
import cn.usian.service.SetmealService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private SetmealDishMapper setmealDishMapper;
    @Override
    public R<Page> page(int page, int pageSize, String name) {
//        Page<Setmeal> page1 = new Page<>(page,pageSize);
//        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(name!=null,Setmeal::getName,name);
//        Page<Setmeal> setmealPage = setmealMapper.selectPage(page1, queryWrapper);
        Page<Setmeal> page1 = new Page<>(page,pageSize);
        Page<SetmealDto> page2=setmealMapper.findPage(page1,name);
        return R.success(page2);
    }

    @Override
    public void status(Integer status, Long[] ids) {
        Setmeal setmeal = new Setmeal();
        for (Long id : ids) {
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmealMapper.updateById(setmeal);
        }
    }

    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            setmealMapper.deleteById(id);
        }
    }
    @Override
    public void add(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateTime(setmealDto.getCreateTime());
            setmealDish.setUpdateTime(setmealDto.getUpdateTime());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
        }
        for (SetmealDish setmealDish : setmealDishList) {
            setmealDishMapper.insert(setmealDish);
        }
    }
}
